import { defineStore } from 'pinia';

export const useUserStore = defineStore('user', {
  state: () => ({
    name: '',
    token: ''
  }),
  getters: {
    isLogin: (state) => !!state.token,
  },
  actions: {
    // 异步登录（直接修改 state，无需 Mutation）
    async login(userInfo) {
      // 模拟接口请求
      const res = await new Promise((resolve) => {
        setTimeout(() => {
          resolve({
            name: userInfo.name,
            token: 'pinia_token_456'
          });
        }, 1000);
      });
      this.name = res.name;
      this.token = res.token;
      return '登录成功';
    },
    // 同步修改状态
    logout() {
      this.name = '';
      this.token = '';
    }
  }
});